package org.jaaksi.demo.animator;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * Created by Fcy on 2015/10/1.<br/>
 * 往返火车票的切换动画实现
 */
public class ExchangePosAnimator extends Activity {

    private Button btnFrom;
    private Button btnTO;
    private int distance = 0;
    private final int margin = 40;
    private final int height = 50;
    /**
     * 是否已经切换过
     */
    private boolean isChanged = false;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animator_exchangepos);

        // 如果是控件不是固定尺寸，则需要测量
        distance = DensityUtil.dip2px(this, 50 + 40);

        btnFrom = (Button) findViewById(R.id.from);
        btnTO = (Button) findViewById(R.id.to);
    }

    public void exchange(View view) {
        if (!isChanged) {
            // fixme 如果values…参数只设置了一个值的话，那么会假定为目的值，属性值的变化范围为当前值到目的值
            // 这里values的距离是相对值，相对原始位置，即原始位置为0
            ObjectAnimator.ofFloat(btnFrom, "TranslationY", distance).setDuration(500).start();
            ObjectAnimator.ofFloat(btnTO, "TranslationY", -distance).setDuration(500).start();
        } else {
            ObjectAnimator.ofFloat(btnFrom, "TranslationY", 0).setDuration(500).start();
            ObjectAnimator.ofFloat(btnTO, "TranslationY", 0).setDuration(500).start();
        }
        isChanged = !isChanged;
    }
}